// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Civilian sabotage friendly vehicles
// -----------------------------------------------

private [
	"_needpropagander",
	"_position",
	"_target",
	"_missioncomplete",
	"_unit"
];

_unit = _this select 0;

_needpropagander = true;

{
	if (_x distance _unit < 500) then {
		_needpropagander = false;
	};
	if (isNull _x) then {wcpropagander = wcpropagander - [_x]};
} foreach wcpropagander;

if (!_needpropagander) exitWith {};

wcpropagander = wcpropagander + [_unit];

diag_log format ["WARCONTEXT: BUILD 1 SABOTER - FAME: %1", wcfame];

_position = (position _unit) findEmptyPosition [8, 100];
if (_position select 0 == 0) then {
	diag_log "WARCONTEXT: CANNOT FIND EMPTY POSITION FOR CIVILIAN SABOTER";
} else {
	_unit setPos _position;
};

WC_fnc_findtargetvehicle = {
	private ["_unit","_target","_vehicles"];

	_unit = _this select 0;

	_target = objNull;
	_vehicles = nearestObjects [_unit, ["LandVehicle"], 600];
	{
		if !(vehicleVarName _x == "") then {
			if (format ["%1", (_x getVariable "wcsabotage")] == "<null>") then {
				_target = _x;
			};
		};
	} foreach _vehicles;

	_target;
};

_target = [_unit] call WC_fnc_findtargetvehicle;

_missioncomplete = false;

while {((alive _unit) and (!_missioncomplete))} do {
	if !(isNull _target) then {
		if (west countSide (nearestObjects [_target, ["Man"], 30]) > 0) then {
			_unit setVariable ["civilrole", "civil", true];
		} else {
			_unit setVariable ["civilrole", "saboter", true];
		};
		_unit setVariable ["target", _target, false];
		if (_target distance _unit < 600) then {
			if (_unit distance _target > 7) then {
				_unit stop false;
				_position = position _target;
				_unit doMove _position;
				_unit setVariable ["destination", _position, false];
			} else {
				for "_i" from 0 to 4 do {
					if (alive _unit) then {
						_unit doWatch _target;
						sleep 3;
						_unit playMove "AinvPknlMstpSnonWnonDnon_medic_2";
						sleep 5;
					};
					_unit playMove "";
				};
				if ((alive _unit) and (_unit distance _target < 10)) then {
					wcgarbage = [_target] spawn WC_fnc_nastyvehicleevent;
					_target setVariable ["wcsabotage", "saboted", false];
					_unit stop false;
					_target = [_unit] call WC_fnc_findtargetvehicle;
				};
			};
		};
	} else {
		_target = [_unit] call WC_fnc_findtargetvehicle;
		if (isNull _target) then {
			_unit setVariable ["civilrole", "civil", true];
		} else {
			_unit setVariable ["civilrole", "saboter", true];
		};
	};
	if ((getMarkerPos "jail") distance _unit < 50) then {
		wcmessageW = ["Saboter", "A prisonner is in jail"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		_unit setPos getMarkerPos "jail";
		[_unit] joinSilent group prisoner;
		_unit allowDammage false;
		_unit setUnitPos "Up";
		_unit playMoveNow "AmovPercMstpSnonWnonDnon_AmovPercMstpSsurWnonDnon";
		_unit stop true;
		_unit disableAI "MOVE";
		_unit disableAI "ANIM";
		_missioncomplete = true;
	};
	sleep 5;
};